﻿using MvcPaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;

namespace zichan.BLL
{
   public class ClassLogService
    {
        public static void AddLog(string className,string operation)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                ClassLog model = new ClassLog();
                model.ClassName = className;
                model.Operation = operation;
                model.Time = DateTime.Now;
                db.ClassLogs.Add(model);
                db.SaveChanges();
            }
        }
        public static IPagedList<ClassLog> ClassLogsList(string className, string keyWord, DateTime? sdt, DateTime? edt, int pageSize, int pageIndex)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<ClassLog> w = db.ClassLogs;
                if (!string.IsNullOrEmpty(className))
                    w = w.Where(o => o.ClassName == className);
                if (!string.IsNullOrEmpty(keyWord))
                    w = w.Where(o => o.ClassName.Contains(keyWord) || o.Operation.Contains(keyWord));
                if (sdt.HasValue)
                    w = w.Where(o => o.Time >= sdt);
                if (edt.HasValue)
                {
                    edt = DateTime.Parse(string.Format("{0:yyyy-MM-dd 23:59:59}", edt));
                    w = w.Where(o => o.Time <= edt);
                }
                return w.OrderByDescending(o => o.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList().ToPagedList(pageIndex - 1, pageSize, w.Count());

            }
        }

    }
}
